package com.illtamer.infinite.sillage.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

public class BaseMapperService<Entity> {

    protected final BaseMapper<Entity> mapper;

    public BaseMapperService(BaseMapper<Entity> mapper) {
        this.mapper = mapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean saveEntity(Entity entity) {
        return mapper.insert(entity) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    public Entity getEntity(Integer id) {
        return mapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public Page<Entity> listEntities(int currentPage, int limit) {
//        PageHelper
        return mapper.selectPage(new Page<>(currentPage, limit), null);
    }

    @Transactional(rollbackFor = Exception.class)
    public Entity updateEntity(Integer id, Entity entity) {
        Entity origin = mapper.selectById(id);
        if (origin == null) return null;
        mapper.updateById(entity);
        return origin;
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteEntity(Integer id) {
        return mapper.deleteById(id) > 0;
    }
}
